package com.company.stringdemo;

import java.util.Scanner;

public class StringDemo7 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int money;
        while (true) {
            System.out.println("请输入一个金额");
            money = sc.nextInt();
            if (money >= 0 && money <= 9999999) {
                break;
            } else {
                System.out.println("您输入的数字不合法");
            }
        }

        System.out.println(money);


        // 将转化金额为汉字
        String HanChinese = getCapitalNumber(money);
        System.out.println(HanChinese);
    }

    public static String getCapitalNumber(int money) {
        String[] ChineseMoney= {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String[] cnMoney = {"佰", "拾", "万", "仟", "佰", "拾", "圆"};

        String capitalStr= "";
        while (money > 0) {
            int getEndNumber = money % 10;
            money = money / 10;

            capitalStr = ChineseMoney[getEndNumber] + capitalStr;

        }

        // 获取剩余字符数
        int lengthSur = 7 - capitalStr.length();
        for (int i = 0; i < lengthSur; i++) {
            capitalStr = "零" + capitalStr;
        }

        String result = "";
        for (int i = 0; i < 7; i++) {
            char ch = capitalStr.charAt(i);
            result = result + ch + cnMoney[i];
        }

        return result;
    }
}
